# -*- coding:utf-8 -*-

from mitmproxy import ctx
import json
from pymongo import MongoClient
from datetime import datetime
import time


class DB(object):
    def __init__(self):
        self.client = MongoClient('127.0.0.1', 27017)
        self.db = self.client["DouYin"]
        self.collection = self.db["快手_用户"]
        self.collection1 = self.db['快手_作品']
    def insertItem(self, data):
        self.collection.insert_one(data)
    def insertItem1(self, data):
        self.collection1.insert_one(data)


    def update_one(self, user_id):
        try:
            result = self.db["快手_用户"].find_one({'user_id': user_id})
            self.db["快手_作品"].update_many({'user_id': user_id},
                                      {'$set':{'cityName': result['cityName'], 'user_text': result['user_text'],
                                               'verified': result['verified'],'fan': result['fan'],
                                               'follow': result['follow'], 'photo_public': result['photo_public'],
                                               'moment': result['moment'], 'atTab': result['atTab']}})
        except:
            print("=======更新失败============")
db = DB()

# mitmdump.exe -p 8888 -s E:\Workspaces\Python\Spider\kuaishou\kuaishou_user_script.py
# 所有发出的请求数据包都会被这个方法所处理
# 所谓的处理，我们这里只是打印一下一些项；当然可以修改这些项的值直接给这些项赋值即可
def request(flow):
    # # 获取请求对象
    # request = flow.request
    # # 实例化输出类
    # info = ctx.log.info
    # # 打印请求的url
    # info(request.url)
    # # 打印请求方法
    # info(request.method)
    # # 打印host头
    # info(request.host)
    # # 打印请求端口
    # info(str(request.port))
    # # 打印所有请求头部
    # info(str(request.headers))
    # # 打印cookie头
    # info(str(request.cookies))
    pass

# 所有服务器响应的数据包都会被这个方法处理
# 所谓的处理，我们这里只是打印一下一些项
def response(flow):
    # 获取响应对象
    response = flow.response
    # # 实例化输出类
    # info = ctx.log.info
    # # 打印响应码
    # info(str(response.status_code))
    # # 打印所有头部
    # info(str(response.headers))
    # # 打印cookie头部
    # info(str(response.cookies))
    # # 打印响应报文内容
    # info(str(response.text))
    try:
        # 获取V用户信息GET
        # if 'aweme-eagle-hl.snssdk.com/aweme/v1/user/' in flow.request.url:
        if 'rest/n/user/profile' in flow.request.url:
            ctx.log.info("============获取数据=============")
            datajson = json.loads(response.text)
            dict_data = dict()
            dict_data['cityCode'] = datajson['userProfile']['cityCode']  # 城市编码
            dict_data['cityName'] = datajson['userProfile']['cityName']  # 城市名称
            dict_data['age'] = datajson['userProfile'].get('age')  # 年龄
            dict_data['constellation'] = datajson['userProfile'].get('constellation')  # 星座
            dict_data['user_id'] = datajson['userProfile']['profile']['user_id']  # 用户昵称
            dict_data['user_name'] = datajson['userProfile']['profile']['user_name']  # 用户昵称
            dict_data['user_sex'] = datajson['userProfile']['profile']['user_sex']  # 用户昵称
            dict_data['user_text'] = datajson['userProfile']['profile']['user_text']  # 简介
            dict_data['verified'] = datajson['userProfile']['profile']['verified']  # 是否认证
            dict_data['fan'] = datajson['userProfile']['ownerCount']['fan']  # 粉丝数量
            dict_data['follow'] = datajson['userProfile']['ownerCount']['follow']  # 关注数量
            dict_data['photo_public'] = datajson['userProfile']['ownerCount']['photo_public']  # 作品数量
            dict_data['moment'] = datajson['userProfile']['ownerCount']['moment']  # 动态数量
            if datajson['userProfile'].get('adBusinessInfo') is None:
                dict_data['atTab'] = ''
            elif datajson['userProfile'].get('adBusinessInfo').get('atTab') is not None:
                dict_data['atTab'] = datajson['userProfile']['adBusinessInfo']['atTab']['count']  # @他数量

            if datajson['userProfile'].get('adBusinessInfo') is None:
                dict_data['tel'] = ""
            elif datajson['userProfile'].get('adBusinessInfo').get('phoneInfo') is not None:
                dict_data['tel'] = datajson['userProfile']['adBusinessInfo']['phoneInfo']['phoneList'][0]['phone'] # 电话
            db.insertItem(dict_data)
            # print(str(dict_data))
            ctx.log.info("============Insert=============")
    except Exception as ex:
        ctx.log.warn("============错误用户=============")
        ctx.log.error(ex)



    try:
        # 获取视频信息-作品
        if 'rest/n/feed/profile2' in flow.request.url:
            ctx.log.info("============获取数据=============")
            datajson = json.loads(response.text)
            for d in datajson['feeds']:
                dict_data = dict()
                dict_data['user_id'] = d.get('user_id')  # 用户id
                dict_data['user_name'] = d.get('user_name')  # 用户昵称
                dict_data['user_sex'] = d.get('user_sex')  # 性别
                dict_data['caption'] = d.get('caption')  # 内容
                dict_data['like_count'] = d.get('like_count')  # 点赞数量
                dict_data['comment_count'] = d.get('comment_count')  # 评论数量
                dict_data['share_count'] = d.get('share_count')  # 分享数量
                dict_data['view_count'] = d.get('view_count')  # 观看数量
                dict_data['duration'] = d.get('duration')  # 视频时长
                dict_data['create_time'] = d.get('time')

                db.insertItem1(dict_data)
                # db.update_one(dict_data['user_id'])

            # print(str(dict_data))
            ctx.log.info("============Insert=============")
    except Exception as ex:
        ctx.log.warn("============错误作品=============")
        ctx.log.error(ex)


    # try:
    #     # 获取视频信息-三农
    #     if 'rest/wd/boutiqueChannel/hotPhoto' in flow.request.url:
    #         ctx.log.info("============获取数据=============")
    #         datajson = json.loads(response.text)
    #         for d in datajson['photos']:
    #             dict_data = dict()
    #             dict_data['user_id'] = d['userId']  # 用户id
    #             dict_data['user_name'] = d['userName']  # 用户昵称
    #             dict_data['user_sex'] = d['userSex']  # 性别
    #             dict_data['caption'] = d['caption']  # 内容
    #             dict_data['like_count'] = d['likeCount']  # 点赞数量
    #             dict_data['comment_count'] = d['commentCount']  # 评论数量
    #             dict_data['view_count'] = d['viewCount']  # 观看数量
    #             dateArray = datetime.fromtimestamp(int(d['timestamp']) / 1000)  # 时间
    #             dict_data['create_time'] = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    #
    #             # db.update_one(dict_data['uid'], dict_data['desc'], dict_data['comment_count'], dict_data['digg_count'],
    #             #               dict_data['share_count'], dict_data['shoot_duration'], dict_data['create_time'])
    #             db.insertItem1(dict_data)
    #         # print(str(dict_data))
    #         ctx.log.info("============Insert=============")
    # except Exception as ex:
    #     ctx.log.warn("============错误作品=============")
    #     ctx.log.error(ex)